package com.stone.business.weekplan.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import net.sf.json.JSON;
import net.sf.json.JSONSerializer;
import net.sf.json.JsonConfig;

import com.stone.business.weekplan.pojo.TWeekplan;
import com.stone.business.weekplan.service.WeekplanService;
import com.stone.sys.pojo.TOrgan;
import com.stone.utils.DateFormatTool;
import com.stone.utils.JsonUtils;
import com.stone.utils.StringUtils;

import frm.ssh.action.BaseListAction;

public class WeekplanListAction extends BaseListAction {
	private static final long serialVersionUID = 7786171678103065000L;
	private WeekplanService weekplanService;
	private String currentNy;
	private String currentWeeknum;
	private String month;
	private List<TOrgan> organList;
	
	/** * 构造方法. */
    public WeekplanListAction() {
        this.entityClass = TWeekplan.class;
    }
    
	public WeekplanService getWeekplanService() {
		return weekplanService;
	}
	public void setWeekplanService(WeekplanService weekplanService) {
		this.weekplanService = weekplanService;
		this.service=weekplanService;
	}
	
	public HashMap<String, Object> setSearchCondition() {
		HashMap<String, Object> m = new HashMap<String, Object>();
		List<Object> valsList = new ArrayList<Object>();
		//String organCode = String.valueOf(super.getSession().getAttribute(IConstant.SESSION_TORGAN_CODE));
		
		m.put("cols", "new map(weekplan.id as id,weekplan.ny as ny,weekplan.weeknum as weeknum,weekplan.organname as organname,weekplan.tbr as tbr,TO_CHAR(weekplan.tbrq,'YYYY-MM-DD') as tbrq)");
		String conditions = " 1 = 1 ";
		
		if (!StringUtils.isNullOrBlankFull(super.getSearchBean().getCon2())){
			conditions += " AND TO_CHAR(weekplan.tbrq,'yyyy-MM-dd') >= ?";
			valsList.add(super.getSearchBean().getCon2());
		}
		
		if (!StringUtils.isNullOrBlankFull(super.getSearchBean().getCon3())){
			conditions += " AND TO_CHAR(weekplan.tbrq,'yyyy-MM-dd') <= ?";
			valsList.add(super.getSearchBean().getCon3());
		}
		
		if (!StringUtils.isNullOrBlankFull(super.getSearchBean().getCon4())){
			conditions += " AND weekplan.organid = ?";
			valsList.add(super.getSearchBean().getCon4());
		}
		
		if (!StringUtils.isNullOrBlank(super.getSearchBean().getCon11())) {
			conditions += " and to_char(weekplan.tbrq,'YYYY-MM') = '" + super.getSearchBean().getCon11() + "'";
		}
		if (!StringUtils.isNullOrBlank(super.getSearchBean().getCon12())) {
			conditions += " and weekplan.weeknum = '" + super.getSearchBean().getCon12() + "'";
		}
		m.put("conditions",conditions);
		m.put("clause"," order by weekplan.tbrq asc");
		m.put("alias", "weekplan");//	m.put("alias", "s, TBdz bdz");
		m.put("vals", valsList.toArray());
		
		getOgList();
		
		return m;
	}


	public String getCurrentNy() {
		return currentNy;
	}

	public void setCurrentNy(String currentNy) {
		this.currentNy = currentNy;
	}

	public String getCurrentWeeknum() {
		return currentWeeknum;
	}

	public void setCurrentWeeknum(String currentWeeknum) {
		this.currentWeeknum = currentWeeknum;
	}

	public String getMonth() {
		return month;
	}

	public void setMonth(String month) {
		this.month = month;
	}

	public List<TOrgan> getOrganList() {
		return organList;
	}

	public void setOrganList(List<TOrgan> organList) {
		this.organList = organList;
	}

	//@SuppressWarnings("unchecked")
	public String toWeekTree() {
		String sysdate = weekplanService.getSysdate("yyyy-MM-dd");
		int weeknum = DateFormatTool.weekOfMonth(DateFormatTool.formatToDate(sysdate, false), false);
		
		this.setCurrentNy(sysdate.substring(0, 7));
		this.setCurrentWeeknum(weeknum + "");
		
		List<HashMap<String, Object>> l = weekplanService.findListBySql("select id as monthid, to_char(tbrq, 'YYYY-MM') as currentmonth from tab_weekplan where TO_CHAR(tbrq, 'yyyy-MM') = TO_CHAR(sysdate, 'yyyy-MM')  ");

		return "TREE_SUCCESS";
	}
	
	@SuppressWarnings("unchecked")
	public void getWeek() throws Exception {
		
		List<HashMap<String,Object>> listTree;
		
		// 左侧的树
		if ("-1".equals(month)) {
			listTree = weekplanService.findListBySql("select to_char(tbrq,'YYYY-MM') as text, to_char(tbrq,'YYYY-MM') as id, 0 as leaf from tab_weekplan group by to_char(tbrq,'YYYY-MM') order by id");

		// 维护时根节点
		} else if (month.length() == 7) {
			listTree = weekplanService.findListBySql("select weeknum as text, '" + month + "' || '-' || weeknum as id, 1 as leaf  from tab_weekplan where to_char(tbrq,'YYYY-MM') = '" + month + "' group by weeknum order by id");
		}else{
			listTree = weekplanService.findListBySql("select weeknum as text, '" + month + "' || '-' || weeknum as id, 1 as leaf  from tab_weekplan where to_char(tbrq,'YYYY-MM') = '" + month + "' group by weeknum order by id");
		}
        JsonConfig jsonConfig = JsonUtils.configJson(new String[] {}, "yyyy-MM-dd");
    	
        JSON json = JSONSerializer.toJSON(listTree, jsonConfig);
        
        super.getResponse().getWriter().print(json.toString());
	}
	
	@SuppressWarnings("unchecked")
	private void getOgList(){
		this.organList = weekplanService.findListByHql(" from TOrgan order by id");
	}
}
